
#import "LLNavViewController.h"
#import "LLBasePublishViewController.h"
#import "AppDelegate.h"

@interface LLNavViewController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation LLNavViewController

+ (void)initialize {
//    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:[LLNavViewController class], nil];
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[LLNavViewController class]]];
    NSMutableDictionary *normalAttr = [NSMutableDictionary dictionary];
//    normalAttr[NSForegroundColorAttributeName] = [UIColor whiteColor];
    normalAttr[NSFontAttributeName] = LLRegularFont(16);
    [item setTitleTextAttributes:normalAttr forState:UIControlStateNormal];

    NSMutableDictionary *disableAttr = [NSMutableDictionary dictionary];
//    disableAttr[NSForegroundColorAttributeName] = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    disableAttr[NSFontAttributeName] = normalAttr[NSFontAttributeName];
    [item setTitleTextAttributes:disableAttr forState:UIControlStateDisabled];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.translucent = NO;
    //barTintColor和BackgroundImage只需要设置其中一个就行了

    self.navigationBar.barTintColor = LLColor(227, 15, 29);
//    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"底"] forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.tintColor = LLWhiteColor;
    [self.navigationBar setShadowImage:[UIImage new]];
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
                                                NSFontAttributeName:LLRegularFont(18)}];

    MJWeakSelf
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
        self.delegate = weakSelf;
    }
    
//    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLTabBarH)];
//    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
//    gradientLayer.colors = @[(__bridge id)LLMainColor.CGColor, (__bridge id)[LLColor(126, 255, 209) CGColor]];
//    gradientLayer.locations = @[@0.3, @1.0];
//    gradientLayer.startPoint = CGPointMake(0, 0);
//    gradientLayer.endPoint = CGPointMake(1.0, 0);
//    gradientLayer.frame = backView.frame;
//    [backView.layer addSublayer:gradientLayer];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_black"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        viewController.navigationItem.leftBarButtonItem = back;
    }
    viewController.view.backgroundColor = LLColor(242, 243, 244);
    
    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES ) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
    // 修改tabBar的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated {
    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    return [super popToRootViewControllerAnimated:animated];
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    return [super popToViewController:viewController animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animate {
    if ([viewController isKindOfClass:[LLBasePublishViewController class]] ) {
        TabBarViewController *tabbarVC = [[TabBarViewController alloc]init];
        AppDelegate *appdelegateE = (AppDelegate*)[UIApplication sharedApplication].delegate;
        appdelegateE.window.rootViewController = tabbarVC;
        tabbarVC.selectedIndex = 3;
    } else {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        if (self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0] ) {
            return NO;
        }
    }
    return YES;
}

- (void)back {
    [self popViewControllerAnimated:YES];
}

@end
